package com.Blog.test_2_1_spring_blog.service;

import com.Blog.test_2_1_spring_blog.mapper.BlogInfoMapper;
import com.Blog.test_2_1_spring_blog.mapper.UserInfoMapper;
import com.Blog.test_2_1_spring_blog.model.BlogInfo;
import com.Blog.test_2_1_spring_blog.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private BlogInfoMapper blogInfoMapper;
    public UserInfo queryByName(String userName){
        return userInfoMapper.queryByName(userName);
    }

    public UserInfo queryById(Integer userId) {
        return  userInfoMapper.queryById(userId);
    }

    public UserInfo getAuthorInfo(Integer blogId) {
        // 1. 根据 博客 id 获取 userId
        BlogInfo blogInfo =  blogInfoMapper.queryById(blogId);
        if(blogInfo == null || blogInfo.getUserId() < 0){
            return null;
        }
        // 2. 根据 userId 获取 userInfo

        return userInfoMapper.queryById(blogInfo.getUserId());
    }
}
